package com.runfig.springcloud.orderfigserver.controller;


import com.netflix.hystrix.contrib.javanica.annotation.HystrixCommand;
import com.runfig.springcloud.domain.Order;
import com.runfig.springcloud.domain.Product;
import com.runfig.springcloud.fegin.ProductFeignApi;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.client.RestTemplate;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.util.UUID;

@RestController
@Slf4j
public class OrderController {

    @Autowired
    private ProductFeignApi productFeignApi;

//    @Resource
//    private RestTemplate restTemplate;

    public static final String PAYMENT_URL = "http://PRODUCT-SERVER";

    @GetMapping("/api/orders")
    @HystrixCommand(fallbackMethod = "failMethod")
    public Order orders(Long userId, Long productId , HttpServletRequest request) {
        log.info("牛逼");
        String cookie = request.getHeader("cookie");
        System.out.println("cookie:"+cookie);
//        if (productId%2 == 0) {
//            int i = 1 / 0;
//        }

//        try {
//            Thread.sleep(10000);
//        } catch (InterruptedException e) {
//            e.printStackTrace();
//        }

        Product product = productFeignApi.get(productId);
        //Product product = restTemplate.getForObject(PAYMENT_URL + "/product/get/2", Product.class);
        Order order = new Order();
        order.setUserId(userId);
        order.setOrderNo(UUID.randomUUID().toString());
        order.setProductName(product.getName());
        order.setProductPrice(product.getPrice());
        order.setProductId(product.getId());
        return order;
    }


    public Order failMethod(Long userId, Long productId,HttpServletRequest request) {
        System.out.println("ssssssssss");
        return new Order();
    }


}
